package dxn.nl.file.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dxn.nl.file.biz.ExportStatusEnum;
import dxn.nl.file.biz.dao.ExcelExportDao;
import dxn.nl.file.biz.po.FileExportApplyDetail;
import dxn.nl.file.biz.service.ExportFileBaseService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ExportFileBaseServiceImpl extends ServiceImpl<ExcelExportDao, FileExportApplyDetail> implements ExportFileBaseService {
    @Override
    public int updateStatus(Long id, ExportStatusEnum statusEnum, int lastDealRow, String message) {
        return updateStatus(id,statusEnum,lastDealRow,message,null);
    }

    @Override
    public int updateStatus(Long id, ExportStatusEnum statusEnum, int lastDealRow, String message, List<String> fileUrl) {
        FileExportApplyDetail fileDetail = new FileExportApplyDetail();
        fileDetail.setStatus(statusEnum.code);
        fileDetail.setTotalRows(lastDealRow);
        fileDetail.setFileUrl(fileUrl);

        if(message == null)
        {
            message = "";
        }
        fileDetail.setMessage(message);

        if(ExportStatusEnum.start_deal == statusEnum)
        {
            fileDetail.setStartTime(LocalDateTime.now());
        }
        if(ExportStatusEnum.success == statusEnum || ExportStatusEnum.deal_exception == statusEnum)
        {
            fileDetail.setEndTime(LocalDateTime.now());
        }

        LambdaQueryWrapper<FileExportApplyDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FileExportApplyDetail::getId, id);

        return this.baseMapper.update(fileDetail,lambdaQueryWrapper);
    }

    @Override
    public int updateDealRows(Long id, int lastDealRow) {
        FileExportApplyDetail fileDetail = new FileExportApplyDetail();
        fileDetail.setId(id);
        fileDetail.setTotalRows(lastDealRow);

        LambdaQueryWrapper<FileExportApplyDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FileExportApplyDetail::getId, id);

        return this.baseMapper.update(fileDetail,lambdaQueryWrapper);
    }

    @Override
    public List<FileExportApplyDetail> selectNeedDealRow(String tag) {
        List<FileExportApplyDetail> list = this.baseMapper.selectConsumeList(tag);
        return list;
    }
}
